import time
from selenium.webdriver.common.by import By

from common.Ulitys import ForumDriver

class ForumLogin:
    driver=""
    url=""
    def __init__(self):
        self.driver=ForumDriver.driver
        self.url="http://127.0.0.1:9580/sign-in.html"
        self.driver.get(self.url)

    def LoginSuccTest(self):
        # 清理输入框
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        # 输入正确的账号和密码
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("kongyan")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        # 等待两秒加载，查看主页元素是否存在
        time.sleep(2)
        self.driver.find_element(By.CSS_SELECTOR,"#article_list_board_title")
        # 截图保存
        ForumDriver.getScreenShot()
        
    def LoginFailTest(self):
        # 清理输入框
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        # 输入错误的账号和密码
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("dasdasd")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        time.sleep(1)
        # 点击之后，弹出错误提示
        alertText = self.driver.find_element(By.CSS_SELECTOR,"body > div.jq-toast-wrap.bottom-right > div:nth-child(1) > h2").text
        ForumDriver.getScreenShot() # 截图
        assert alertText=="警告"
        
         # 输入正确的用户名、错误的密码
         # 清理输入框
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("kongyan")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("wrongpass")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        time.sleep(1)
        # 点击之后，弹出错误提示
        alertText = self.driver.find_element(By.CSS_SELECTOR,"body > div.jq-toast-wrap.bottom-right > div:nth-child(1) > h2").text
        ForumDriver.getScreenShot() # 截图
        assert alertText=="警告"
        
        # 不输入用户名 只输入密码
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        time.sleep(1)
        ForumDriver.getScreenShot() # 截图
        # 提示：用户名不能为空
        text=self.driver.find_element(By.CSS_SELECTOR,"#signInForm > div.mb-3 > div").text
        ForumDriver.getScreenShot() # 截图
        assert text=="用户名不能为空"
        
        # 不输入密码 只输入用户名
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("kongyan")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        
        ForumDriver.getScreenShot() # 截图
        # 提示：密码不能为空
        text=self.driver.find_element(By.CSS_SELECTOR,"#signInForm > div.mb-2 > div > div").text
        ForumDriver.getScreenShot() # 截图
        assert text=="密码不能为空"
        
# ForumLogin().LoginFailTest()
# ForumLogin().LoginSuccTest()
        
